package com.hspedu.homework;

/**
 * @author Charlie
 * @version 1.0
 * @date 4/11/2025 下午4:36
 */
public class BinaryTreeDemonTest {
    public static void main(String[] args) {
        AlphaNode a = new AlphaNode("A", 1);
        AlphaNode b = new AlphaNode("B", 2);
        AlphaNode c = new AlphaNode("C", 3);
        AlphaNode d = new AlphaNode("D", 4);
        AlphaNode e = new AlphaNode("E", 5);

        BinaryTree1 binaryTree1 = new BinaryTree1(a);
        a.left = b;
        a.right = c;
        b.left=d;
        b.right=e;


       /* System.out.println("===前序遍历===");
        binaryTree1.preOrder(a);

        System.out.println("===中序遍历===");
        binaryTree1.infixOrder(a);

        System.out.println("===后序遍历===");
        binaryTree1.postOrder(a);*/


        AlphaNode alphaNode = binaryTree1.preSearch(5);
        if(alphaNode==null){
            System.out.println("没有找到相关元素");
        }else {
            System.out.println("找到了相关元素是==> "+alphaNode);
        }
    }


}

class BinaryTree1 {
    private AlphaNode root;

    public BinaryTree1(AlphaNode root) {
        this.root = root;
    }
    public BinaryTree1(){};


    public void preOrder(AlphaNode root) {
        if (root == null) {
            System.out.println("无法遍历，为空树");
        } else {
            root.preOrder();
        }
    }

    public void infixOrder(AlphaNode root) {
        if (root == null) {
            System.out.println("无法遍历，为空树");
        } else {
            root.infixOrder();
        }
    }

    public void postOrder(AlphaNode root) {
        if (root == null) {
            System.out.println("无法遍历，为空树");
        } else {
            root.postOrder();
        }
    }

    public AlphaNode preSearch(int no ) {
        if(this.root.getNo()==no){
            System.out.println(root);
            return root;
        }else {
            return this.root.preOrderSearch(no);
        }
    }
}

class AlphaNode {
    private String name;
    private int no;
    AlphaNode left;
    AlphaNode right;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public AlphaNode(String name, int no) {
        this.name = name;
        this.no = no;
    }

    @Override
    public String toString() {
        return "AlphaNode{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }


    /*递归删除结点
    *
    *
    * */



    //前序遍历
    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    //前序search
    public AlphaNode preOrderSearch(int no){
        if(this.no==no){
            System.out.println(this);
            return this;
        }else {
            AlphaNode resNode = null;
            if(this.left!=null){
               resNode= this.left.preOrderSearch(no);
            }
            if(resNode!=null){
                return  resNode;
            }
            if(this.right!=null){
                resNode=this.right.preOrderSearch(no);
            }

            return resNode;

        }


    }

    //中序遍历
    public void infixOrder(){

        if(this.left!=null){
            this.left.infixOrder();
        }

        System.out.println(this);

        if(this.right!=null){
            this.right.infixOrder();
        }
    }

    //后续遍历
    public void postOrder(){
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }


}
